package class06;

import java.util.Stack;

public class Code04_ReverseStackUsingRecursive {
    // 使用递归函数并且不申请额外的空间,逆序栈
    static void resver(Stack<Integer> stack) {
        if (stack.isEmpty()) {
            return;
        }
        // 拿出栈底元素，并将剩下元素往栈底移动
        int i = f(stack);
        resver(stack);
        stack.push(i);

    }

    static int f(Stack<Integer> stack) {
        int pop = stack.pop();
        if (stack.isEmpty()) {
            return pop;
        } else {
            int i = f(stack);
            stack.push(pop);
            return i;
        }
    }

    public static void main(String[] args) {
        Stack<Integer> test = new Stack<Integer>();
        test.push(1);
        test.push(2);
        test.push(3);
        test.push(4);
        test.push(5);
        resver(test);
        while (!test.isEmpty()) {
            System.out.println(test.pop());
        }

    }
}
